package com.itheima.consumer.listener;

import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author db
 * @version 1.0
 * @description SpringRabbitListener
 * @since 2023/11/29
 */

@Component
public class SpringRabbitListener {
//     利用RabbitListener来声明要监听的队列信息
//     将来一旦监听的队列中有了消息，就会推送给当前服务，调用当前方法，处理消息。
//     可以看到方法体中接收的就是消息体的内容
//    @RabbitListener(queuesToDeclare = @Queue(value = "simple.queue"))
//    public void listenSimpleQueueMessage(String msg) throws InterruptedException {
//        System.out.println("spring 消费者接收到消息：【" + msg + "】");
//        throw new MessageConversionException("故意的");
//    }
//
//
//    @RabbitListener(queuesToDeclare = @Queue(value = "work.queue"))
//    public void listenWorkQueueMessage1(String msg) throws InterruptedException {
//        System.out.println("消费者1接收到消息：【" + msg + "】");
//        Thread.sleep(200);
//    }
//
//    @RabbitListener(queuesToDeclare = @Queue(value = "work.queue"))
//    public void listenWorkQueueMessage2(String msg) throws InterruptedException {
//        System.out.println("消费者2接收到消息.............：【" + msg + "】");
//        Thread.sleep(20);
//    }

//    @RabbitListener(bindings = @QueueBinding(value = @Queue, // 创建临时队列
//    exchange = @Exchange(name="logs",type="fanout")  // 绑定交换机
//    ))
//    public void listenFanoutQueueMessage1(String msg) throws InterruptedException {
//        System.out.println("消费者1接收到消息：【" + msg + "】");
//    }
//
//    @RabbitListener(bindings = @QueueBinding(value = @Queue, // 创建临时队列
//            exchange = @Exchange(name="logs",type="fanout")  // 绑定交换机
//    ))
//    public void listenFanoutQueueMessage2(String msg) throws InterruptedException {
//        System.out.println("消费者2接收到消息。。。。：【" + msg + "】");
//    }



    @RabbitListener(bindings = @QueueBinding(value = @Queue, // 创建临时队列
            key = {"info","error"},  //路由key
            exchange = @Exchange(name="logs",type="direct")  // 绑定交换机
    ))
    public void listenDirectQueueMessage1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue, // 创建临时队列
            key = {"error"},  //路由key
            exchange = @Exchange(name="logs",type="direct")  // 绑定交换机
    ))
    public void listenDirectQueueMessage2(String msg) throws InterruptedException {
        System.out.println("消费者2接收到消息。。。。：【" + msg + "】");
    }


    @RabbitListener(bindings = @QueueBinding(value = @Queue, // 创建临时队列
            key = {"user.*"},  //路由key
            exchange = @Exchange(name="topics",type="topic")  // 绑定交换机
    ))
    public void listenTopicQueueMessage1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue, // 创建临时队列
            key = {"user.#"},  //路由key
            exchange = @Exchange(name="topics",type="topic")  // 绑定交换机
    ))
    public void listenTopicQueueMessage2(String msg) throws InterruptedException {
        System.out.println("消费者2接收到消息：【" + msg + "】");
    }


    @RabbitListener(queues = "object.queue")
    public void listenObjectMessage(Map<String,Object> msg) throws InterruptedException {
        System.out.println("消费者接收到消息：【" + msg + "】");
        throw new RuntimeException("故意的");
    }

//    @RabbitListener(bindings = @QueueBinding(value = @Queue, // 创建临时队列
//            key = {"user.#"},  //路由key
//            exchange = @Exchange(name="topics",type="topic"),  // 绑定交换机
//            arguments = @Argument(name="x-queue-mod",value = "lazy")
//    ))
//    public void listenLazyQueueMessage(String msg) throws InterruptedException {
//        System.out.println("消费者2接收到消息：【" + msg + "】");
//    }

}
